package com.ctshk.rpc.order.goods.mapper;

import com.ctshk.rpc.order.goods.base.CustomBaseMapper;
import com.ctshk.rpc.order.goods.entity.CommodityCost;
import com.ctshk.rpc.order.goods.req.conmodity.CommodityCostReq;
import com.ctshk.rpc.order.goods.req.conmodity.CommodityCostSingleReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品成本表 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-13
 */
public interface CommodityCostMapper extends CustomBaseMapper<CommodityCost> {

    @Select({
            "<script>",
            "SELECT",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.cost_price, t.gmt_create,t.create_id,t.gmt_modified,t.modified_id,t.is_deleted  ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='commodityCostAttributeIds != null and commodityCostAttributeIds.size != 0'>",
            "AND t.cost_attribute_id IN ",
            "<foreach collection='commodityCostAttributeIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<CommodityCost> findCommodityCostByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId,
                                                       @Param("commodityCostAttributeIds") List<Long> commodityCostAttributeIds);

    @Select({
            "<script>",
            "SELECT ",
            "t.id, t.commodity_attribute_id, t.commodity_id, t.cost_attribute_id, t.cost_price, t.picture_url, t.profit, t.price, t.marking_price, t.settlement_price, t.stock, t.weight, t.taxation, t.number ",
            ", ca.id AS \"commodityCostAttributeReq.id\",  ca.commodity_attribute_id AS \"commodityCostAttributeReq.commodity_attribute_id\",  ca.commodity_id AS \"commodityCostAttributeReq.commodity_id\",  ca.specifications_attribute_one_id AS \"commodityCostAttributeReq.specifications_attribute_one_id\",  ca.specifications_attribute_one_name AS \"commodityCostAttributeReq.specifications_attribute_one_name\",  ca.specifications_attribute_two_id AS \"commodityCostAttributeReq.specifications_attribute_two_id\",  ca.specifications_attribute_two_name AS \"commodityCostAttributeReq.specifications_attribute_two_name\" ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "LEFT JOIN `ctshk-goods`.t_commodity_cost_attribute ca ON ca.id = t.cost_attribute_id ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "</script>"
    })
    List<com.ctshk.rpc.order.goods.req.conmodity.CommodityCostSingleReq> findCommodityCostAndAttributeByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId);

    @Select({
            "<script>",
            "SELECT ",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.picture_url,MIN(t.price) as \"price\",t.marking_price",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='specificationsType != null and specificationsType == 1'> AND t.cost_attribute_id IS NULL</if> ",
            "<if test='specificationsType != null and specificationsType == 2'> AND t.cost_attribute_id IS NOT NULL</if> ",
            "</script>"
    })
    CommodityCostReq findCommodityCostReqMinPriceByCombinedIds(@Param("commodityId") Long commodityId,
                                                               @Param("commodityAttributeId") Long commodityAttributeId, @Param("specificationsType") Integer specificationsType);


    // =============================== H5端 Start ===============================
    @Select({
            "<script>",
            "SELECT",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.cost_price, t.gmt_create,t.create_id,t.gmt_modified,t.modified_id,t.is_deleted  ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='commodityCostAttributeIds != null and commodityCostAttributeIds.size != 0'>",
            "AND t.cost_attribute_id IN ",
            "<foreach collection='commodityCostAttributeIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<CommodityCost> findHtmlFiveCommodityCostByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId,
                                                               @Param("commodityCostAttributeIds") List<Long> commodityCostAttributeIds);

    @Select({
            "<script>",
            "SELECT ",
            "t.id, t.commodity_attribute_id, t.commodity_id, t.cost_attribute_id, t.cost_price, t.picture_url, t.profit, t.price, t.marking_price, t.settlement_price, t.stock, t.weight, t.taxation, t.number ",
            ", ca.id AS \"commodityCostAttributeReq.id\",  ca.commodity_attribute_id AS \"commodityCostAttributeReq.commodity_attribute_id\",  ca.commodity_id AS \"commodityCostAttributeReq.commodity_id\",  ca.specifications_attribute_one_id AS \"commodityCostAttributeReq.specifications_attribute_one_id\",  ca.specifications_attribute_one_name AS \"commodityCostAttributeReq.specifications_attribute_one_name\",  ca.specifications_attribute_two_id AS \"commodityCostAttributeReq.specifications_attribute_two_id\",  ca.specifications_attribute_two_name AS \"commodityCostAttributeReq.specifications_attribute_two_name\" ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "LEFT JOIN `ctshk-goods`.t_commodity_cost_attribute ca ON ca.id = t.cost_attribute_id ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "</script>"
    })
    List<CommodityCostSingleReq> findHtmlFiveCommodityCostAndAttributeByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId);

    /**
     * 查询商品属性列表
     * @param id
     * @param id1
     * @param ids
     * @return
     */
    @Select({
            "<script>" +
            "SELECT id,commodity_attribute_id,commodity_id,cost_attribute_id,cost_price,picture_url,profit,price," +
                    "marking_price,settlement_price,stock,weight,taxation,number," +
                    "create_id,modified_id,is_deleted,gmt_create,gmt_modified " +
                    "FROM `ctshk-goods`.t_commodity_cost " +
                    "WHERE commodity_attribute_id = #{id} " +
                    "AND commodity_id = #{commodityId} AND " +
                    "cost_attribute_id IN " +
                    "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                    " #{id}\n" +
                    "</foreach>" +
                    "</script>"
    })
    List<CommodityCost> selectAllList(@Param("id") Long id,@Param("commodityId") Long commodityId,@Param("list") List<Long> ids);

    /**
     * 查询商品成本
     * @param singleCommodityIds
     * @param code
     * @return
     */
    @Select({
            "<script>" +
            "SELECT  id,commodity_attribute_id,commodity_id,cost_attribute_id,cost_price,picture_url," +
                    "profit,price,marking_price,settlement_price,stock,weight,taxation,number,create_id," +
                    "modified_id,is_deleted,gmt_create,gmt_modified  " +
                    "FROM `ctshk-goods`.t_commodity_cost     " +
                    "WHERE (commodity_id IN " +
                    "<foreach collection='singleCommodityIds' item='id' open='(' separator=',' close=')'>",
                    "#{id}",
                    "</foreach>",
                    "AND is_deleted = #{isDeleted})"+
            "</script>"
    })
    List<CommodityCost> selectListByIds(@Param("singleCommodityIds") List<Long> singleCommodityIds,@Param("isDeleted") Integer isDeleted);

    /**
     * 多规格属性查询
     * @param commodityId
     * @param commodityAttributeId
     * @param costAttributeIds
     * @param isDeleted
     * @return
     */
    @Select({
            "<script>" +
            "SELECT  id,commodity_attribute_id,commodity_id,cost_attribute_id,cost_price,picture_url," +
                    "profit,price,marking_price,settlement_price,stock,weight,taxation,number,create_id," +
                    "modified_id,is_deleted,gmt_create,gmt_modified  " +
                    "FROM `ctshk-goods`.t_commodity_cost     " +
                    "WHERE (commodity_id = #{commodityId} " +
                    "AND commodity_attribute_id = #{commodityAttributeId} " +
                    "AND cost_attribute_id IN " +
                    "<foreach collection=\"costAttributeIds\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                    "#{id}\n" +
                    "</foreach>" +
                    "AND is_deleted = #{isDeleted})" +
            "</script>"
    })
    List<CommodityCost> selectByCommodityIdAndAttrId(@Param("commodityId") Long commodityId,@Param("commodityAttributeId") Long commodityAttributeId,@Param("costAttributeIds") List<Long> costAttributeIds, @Param("isDeleted") Integer isDeleted);

    /**
     * 商品id查询商品成本
     * @param id
     * @return
     */
    @Select({
            "SELECT `id`, `commodity_attribute_id`, `commodity_id`, `cost_attribute_id`, `cost_price`, `picture_url`, " +
                    "`profit`, `price`, `marking_price`, `settlement_price`, `stock`, `weight`, `taxation`, `number`, " +
                    "`gmt_create`, `create_id`, `gmt_modified`, `modified_id`, `is_deleted`\n" +
                    "FROM `ctshk-goods`.`t_commodity_cost` \n" +
                    "WHERE commodity_id = #{id}"
    })
    CommodityCost findCommodityCostByCommodityId(@Param("id") Long id);


    // =============================== H5端 End ===============================
}
